package com.maduar.filter.config;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.nashorn.internal.parser.JSONParser;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description:
 *
 * @Author maduar maduar@163.com
 * @Date 2021-08-22 23:44
 */
@Order(0)
@WebFilter(filterName = "ThreadContextRequestFilter", urlPatterns = "/*")
@Configuration
@Log4j2
public class DemoFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        BodyReaderHttpServletRequestWrapper requestWrapper = new BodyReaderHttpServletRequestWrapper(httpServletRequest);

        String s = new String(requestWrapper.getBody());
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(s);
        JsonNode id = jsonNode.get("id");
        log.info("id");

        filterChain.doFilter(requestWrapper, httpServletResponse);
    }
}
